import logging
import itertools
logger = logging.getLogger(__name__)

event_roles = {"质押": ["B_number", "B_obj-per", "B_collateral", "B_date", "B_sub-per", "B_obj-org", "B_proportion", "B_sub-org", "B_money",
                        "I_number", "I_obj-per", "I_collateral", "I_date", "I_sub-per", "I_obj-org", "I_proportion", "I_sub-org", "I_money"],
               "股份股权转让": ["B_number", "B_target-company", "B_obj-per", "B_date", "B_collateral", "B_sub-per", "B_obj-org", "B_proportion", "B_sub-org", "B_money",
                        "I_number", "I_target-company", "I_obj-per", "I_date", "I_collateral", "I_sub-per", "I_obj-org", "I_proportion", "I_sub-org", "I_money"],
               "投资": ["B_money", "B_date", "B_sub", "B_obj",
                        "I_money", "I_date", "I_sub", "I_obj"],
               "减持": ["B_share-per", "B_share-org", "B_obj", "B_date", "B_sub", "B_title",
                        "I_share-per", "I_share-org", "I_obj", "I_date", "I_sub", "I_title"],
               "起诉": ["B_obj-per", "B_date", "B_sub-per", "B_obj-org", "B_sub-org",
                        "I_obj-per", "I_date", "I_sub-per", "I_obj-org", "I_sub-org"],
               "收购": ["B_number", "B_date", "B_sub-per", "B_obj-org", "B_proportion", "B_sub-org", "B_money", "B_way",
                        "I_number", "I_date", "I_sub-per", "I_obj-org", "I_proportion", "I_sub-org", "I_money", "I_way"],
               "判决": ["B_obj-per", "B_date", "B_sub-per", "B_obj-org", "B_sub-org", "B_institution", "B_money",
                        "I_obj-per", "I_date", "I_sub-per", "I_obj-org", "I_sub-org", "I_institution", "I_money"],
               "签署合同": ["B_obj-per", "B_date", "B_amount", "B_sub-per", "B_obj-org", "B_sub-org",
                        "I_obj-per", "I_date", "I_amount", "I_sub-per", "I_obj-org", "I_sub-org"],
               "担保": ["B_date", "B_amount", "B_obj-org", "B_sub-per", "B_sub-org", "B_way",
                        "I_date", "I_amount", "I_obj-org", "I_sub-per", "I_sub-org", "I_way"],
               "中标": ["B_amount", "B_date", "B_sub", "B_obj",
                        "I_amount", "I_date", "I_sub", "I_obj"]}


event_trigger = {
    '质押': ['B_trigger', 'I_trigger'],
    '股份股权转让': ['B_trigger', 'I_trigger'],
    '投资': ['B_trigger', 'I_trigger'],
    '减持': ['B_trigger', 'I_trigger'],
    '起诉': ['B_trigger', 'I_trigger'],
    '收购': ['B_trigger', 'I_trigger'],
    '判决': ['B_trigger', 'I_trigger'],
    '签署合同': ['B_trigger', 'I_trigger'],
    '担保': ['B_trigger', 'I_trigger'],
    '中标': ['B_trigger', 'I_trigger']}

event_type2idx = {'质押':0,'股份股权转让':1,'投资':2,'减持':3,'起诉':4,'收购':5,'判决':6,'签署合同':7,'担保':8,'中标':9}
idx2event_type = {0:'质押',1:'股份股权转让',2:'投资',3:'减持',4:'起诉',5:'收购',6:'判决',7:'签署合同',8:'担保',9:'中标'}

def get_role_role2idx():
    all_trigger_roles = []
    for event_type_t,trigger in event_trigger.items():
        for event_type_r,roles in event_roles.items():
            if event_type_r == event_type_t:
                trigger_role_list = list(itertools.product(trigger,roles))
                role_trigger_list = list(itertools.product(roles,trigger))
                for trigger_role in trigger_role_list:
                    all_trigger_roles.append((event_type_r, trigger_role[0], trigger_role[1]))
                for role_trigger in role_trigger_list:
                    all_trigger_roles.append((event_type_r, role_trigger[0], role_trigger[1]))
            else:
                continue

    trigger_role2idx = {'non_conn':0}

    for idx,trigger_role in enumerate(all_trigger_roles):
        trigger_role2idx[trigger_role] = idx+1
    # print(trigger_role2idx)

    idx2trigger_role = {idx:trigger_role for trigger_role,idx in trigger_role2idx.items()}
    # print(len(idx2trigger_role))

    return trigger_role2idx,idx2trigger_role



def get_stop_words():
    stopwords = []
    with open('./date3/stopwords.txt', 'r', encoding='utf-8') as f_stopword:
        stopword_datas = f_stopword.readlines()
        for stopword in stopword_datas:
            stopwords.append(stopword.strip())
    return stopwords

# if __name__ == "__main__":
#     role_role2idx, idx2role_role = get_role_role2idx()
#     # print(role_role2idx)
#     # print(len(idx2role_role))
